package com.hopedove.workshop.ui.shiftchange

import android.os.Bundle
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.LiveData
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hopedove.base.BaseActivity
import com.hopedove.viewmodel.CommonEvent
import com.hopedove.workshop.R
import com.hopedove.workshop.api.ShiftChangeClothInfo
import com.hopedove.workshop.data.pojo.ObservableShiftRecordItem
import com.hopedove.workshop.databinding.WorkshopShiftChangeRecordActivityBinding
import com.hopedove.workshop.ui.adapter.ShiftChangeRecordAdapter
import com.hopedove.workshop.ui.dialog.ListSelectionDialog
import com.hopedove.workshop.viewmodel.shiftchange.ShiftChangeRecordViewModel
import org.koin.androidx.viewmodel.ext.android.viewModel

class ShiftChangeRecordActivity : BaseActivity() {

    lateinit var binding: WorkshopShiftChangeRecordActivityBinding
    private val viewModel: ShiftChangeRecordViewModel by viewModel()

    private val mAdapter = ShiftChangeRecordAdapter()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding =
            DataBindingUtil.setContentView(this, R.layout.workshop_shift_change_record_activity)
        binding.lifecycleOwner = this
        binding.vm = viewModel

        initViews()
        observeToastText(viewModel.toastText)
        observeLoading(viewModel.loading)
        observeRecords(viewModel.records)
        observeCommonEvent(viewModel.commonEvent)
    }

    private fun initViews() {
        binding.rvList.apply {
            layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
            adapter = mAdapter
        }
    }

    private fun observeRecords(liveData: LiveData<List<ObservableShiftRecordItem>>) {
        liveData.observe(this) { list ->
            mAdapter.setData(list)
        }
    }

    private fun observeCommonEvent(eventLiveData: LiveData<CommonEvent<*>>) {

        eventLiveData.observe(this) { event ->
            when (event.name) {
                "event_select_material" -> {
                    val list = event.data as List<ObservableShiftRecordItem>
                    val dialog = ListSelectionDialog(title = "纱支信息")
                    dialog.mustSelectedToConfirm = true
                    dialog.setData(list.map { item -> ListSelectionDialog.ListItem(text = item.info) })
                    dialog.getSelectedItem = { indexes ->
                        viewModel.onMaterialSelected(indexes)
                    }
                    dialog.show(supportFragmentManager, "select-material")
                }
                "event_select_cloth_info" -> {
                    val list = event.data as List<ShiftChangeClothInfo>
                    val dialog = ListSelectionDialog(false, "布号信息")
                    dialog.mustSelectedToConfirm = true
                    dialog.setData(list.map { item -> ListSelectionDialog.ListItem(text = item.clothNo) })
                    dialog.getSelectedItem = { indexes ->
                        viewModel.onClothInfoSelected(indexes[0])
                    }
                    dialog.show(supportFragmentManager, "select-cloth-info")
                }
            }
        }
    }

}